<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<!doctype html>
<html>
<head>

  <title>history use case</title>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes">
  <meta name="mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-capable" content="yes">

  <script src="../../../webcomponentsjs/webcomponents-lite.js"></script>

  <link rel="import" href="../../../polymer/polymer.html">
  <link rel="import" href="../../../iron-scroll-threshold/iron-scroll-threshold.html">
  <link rel="import" href="../../iron-list.html">

  <style>

    body {
      margin: 0;
    }

  </style>

</head>
<body>
  <dom-module id="my-element">
    <template>
      <style>
        #threshold {
          position: absolute;
          top: 0;
          bottom: 0;
          left: 0;
          right: 0;
          overflow: auto;
        }
        
        iron-list {
          height: 100%;
        }
        
        [item] {
          height: 40px;
          background-color: lightblue;
        }
        
        [item].last {
          margin-bottom: 40px;
        }
      </style>

      <iron-scroll-threshold on-lower-threshold="loadMoreData" id="threshold">
        <iron-list id="list" items="[[items]]" scroll-target="threshold">
          <template>
            <div>
              <div item class$="[[getItemClass(index, items.length)]]">[[index]]</div>
            </div>
          </template>
        </iron-list>
      </iron-scroll-threshold>
    </template>

    <script>
        Polymer({
          is: 'my-element',
          
          properties: {
            items: Array,
          },
          
          loadMoreData: function() {
            console.log('items before: ' + this.$.list._physicalCount);
            setTimeout(() => console.log('items after: ' + this.$.list._physicalCount), 0)
            
            var newItems = [];
            for (var i = 0; i < 30; i++) {
              newItems.push(i);
            }
            
            if (this.items) {
              //return;
              newItems.unshift('items');
              this.push.apply(this, newItems);
            } else {
              this.set('items', newItems);
            }
            this.$.threshold.clearTriggers();
          },
          
          getItemClass: function(index, length) {
            return (index == length - 1) ? 'last' : '';
          }
        });
    </script>
  </dom-module>

  <my-element></my-element>

</body>
</html>
